
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:self32/entity/book_entity.dart';

class BorrowReturnModel{

  final RxList<BookEntity> _list = RxList();
  //0 = borrow  1=return
  var type = 0;

  upDataList(list){
    _list.clear();
    _list.addAll(list);
  }

  _listCell(text,{int width = 90, int height = 20, Color color =Colors.black }){
    return Container(
        alignment: Alignment.center,
        width: width.w,
        height: height.h,
        child: Text('$text',style: TextStyle(color:color, fontSize: 8.sp),textAlign: TextAlign.center,)
    );
  }

  _labelRow(width){
    return  Container(
      height: 30.h,
      decoration: BoxDecoration(
        color: const Color.fromRGBO(219, 232, 255, 1),
        borderRadius: BorderRadius.only(topLeft:Radius.circular(4.r),topRight:Radius.circular(4.r)),
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.start,
        children: [
          _listCell("序号",width: ((50/510)*width).toInt()),
          _listCell("书籍名称",width: ((140/510)*width).toInt()),
          _listCell("书籍编号",width: ((140/510)*width).toInt()),
          _listCell("应还时间",width: ((100/510)*width).toInt()),
          _listCell("状态",width:(( 80/510)*width).toInt()),
        ],
      ),
    );
  }


  Widget show(list,type,callback,{int width = 510,int marginLeft = 10}){
    this.type = type;
    _list.clear();
    _list.addAll(list);
    return  Obx(() => Container(
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.all(Radius.circular(4.r)),
      ),
      margin: EdgeInsets.only(left: marginLeft.w),
      width: width.w,
      height: 310.h,
      child: Column(
        children: [
          _labelRow(width),
          SizedBox(height:4.h),
          SizedBox(
            height: 220.h,
            child: ListView.separated(
              itemBuilder: (context, index) {
                return _itemBuilder(index,width);
              },
              itemCount: _list.length,
              separatorBuilder: (BuildContext context, int index) {
                return Divider(
                  color: Colors.grey[400],
                );
              },
            )
          ),
          SizedBox(height:10.h),
          InkWell(
            onTap: () async {
              callback();
            },
            child: Image.asset("assets/images/${type == 0?'borrow_confirm':'return_confirm'}.png",width: 100.w,fit: BoxFit.fitWidth,),
          ),
        ],
      ),
    ));
  }

  _itemBuilder(index,width){
    var item = _list[index];
    return Row(
      children: [
        _listCell("${index+1}",width: ((50/510)*width).toInt()),
        _listCell("《${item.mainTitle}》",width: ((140/510)*width).toInt()),
        _listCell("${item.no}",width: ((140/510)*width).toInt()),
        _listCell(item.readyReturnDate??'',width: ((100/510)*width).toInt()),
        // _listCell("状态",width: 80),
        Container(
          width: ((80/510)*width).toInt().w,
          alignment: Alignment.center,
          child:  Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              if(type == 0)...[
                Image.asset("assets/images/${item.state==2?'has_borrowed':'borrow_wait'}.png",width: 60.w,fit: BoxFit.fitWidth,),
              ]else...[
                Image.asset("assets/images/${item.state==2?'has_returned':'return_wait'}.png",width: 60.w,fit: BoxFit.fitWidth,),
              ]
            ],
          ),
        )
      ],
    );
  }
}

